

import re
from error import Error404


def choosebyURL(args, url) :
    for pattern,view in args :
        m = re.match(pattern, url)
        if m :
            return view(**m.groupdict())
    raise Error404()


class URLmapper :
    def __init__(self, *args) :
        self.urls = [ (re.compile(i[0]), i[1]) for i in args ]
        
    def run(self, path) :
        for url in self.urls  :
            m = url[0].match(path)
            if m :
                url[1](**m.groupdict())
                return
        print 'ERROR'
